layui.use(['form','layer','table'],function(){
    var form = layui.form,
        layer = layui.layer,
        $ = layui.jquery,
        table = layui.table;
    //添加验证规则
    form.verify({
        searchNotNull : function(value){
            if(value.trim() ==""){
                return "请输入查找内容";
            }
        }
    });

    //部门列表初始化
    var tableIns = table.render({
        elem: '#roleList',
        url : 'json/roleList.json',//TODO
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limits : [10,15,20,25],
        limit : 20,
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'id', title: '角色编号', minWidth:100, align:"center"},
            {field: 'name', title: '角色名称', minWidth:100, align:"center"},
            {field: 'roleType', title: '角色类型', minWidth:200, align:'center'},
            {title: '操作', minWidth:175, templet:'#roleListBar',fixed:"right",align:"center"}
        ]]
    });

    //搜索
    form.on("submit(search)",function () {
        layer.msg($(".searchVal").val());//TODO
    });

    //添加或修改角色公用方法
    function edit(val){
        let temp="添加角色";
        if(val!=null)
            temp="修改角色";
            layer.open({
            title : temp,
            type : 2,
            content : "page/role/add.jsp",
            skin:'layui-layer-molv',
            area: ['600px', '400px'],
            btn:['提交'],
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                if(val){
                    body.find(".name").val(val.name);
                    body.find(".id").val(val.id);
                    body.find(".roleType").val(val.roleType);
                    form.render();
                }
            },
            yes: function (index) {//提交按钮回调方法
                layer.msg("提交中...");//TODO
                layer.close(index);
            }
        })

    };
    //添加角色
    $(".add_btn").click(function () {
        edit();
    });
    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus('deptList'),
            data = checkStatus.data,
            id = [];
        if(data.length > 0) {
            for (var i in data) {
                id.push(data[i].id);
            }
            layer.confirm(`确定删除选中的[${id.length}]个角色？`, {icon: 3, title: '提示信息'}, function (index) {
                tableIns.reload();//TODO
                layer.close(index);
            });
        }else{
            layer.msg("请选择需要删除的角色");
        };
    });
    //列表操作
    table.on('tool(roleList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){ //编辑角色
            edit(data);
        }
        if (layEvent==='del') {//删除角色
            layer.confirm("确定删除此角色["+data.name+"]？",{icon:3, title:'提示信息'},function(index){
                tableIns.reload();//TODO
                layer.close(index);
            });
        }
    });


})